package com.zja.dao.Impl;

import com.zja.dao.BaseDAO;
import com.zja.dao.KnowledgeBaseAdminDao;
import com.zja.model.KnowledgeBaseAdmin;

import java.util.ArrayList;
import java.util.List;

/**
 * @author JA
 * @date: 2025/5/4
 * description:
 */
public class KnowledgeBaseAdminDaoImpl extends BaseDAO implements KnowledgeBaseAdminDao {
    @Override
    public boolean addAdmin(Long knowledgeBaseId, Long userId) {
        String sql = "INSERT INTO knowledgebase_admin (knowledge_base_id, user_id) VALUES (?, ?)";
        try {
            return executeUpdate(sql, knowledgeBaseId, userId);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public boolean removeAdmin(Long knowledgeBaseId, Long userId) {
        String sql = "DELETE FROM knowledgebase_admin WHERE knowledge_base_id = ? AND user_id = ?";
        try {
            return executeUpdate(sql, knowledgeBaseId, userId);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<Long> findAdminIdsByKnowledgeBaseId(Long knowledgeBaseId) {
        String sql = "SELECT user_id FROM knowledgebase_admin WHERE knowledge_base_id = ?";
        try {
            List<KnowledgeBaseAdmin> list = executeQuery(KnowledgeBaseAdmin.class, sql, knowledgeBaseId);
            List<Long> ids = new ArrayList<>();
            for (KnowledgeBaseAdmin kba : list) {
                ids.add(kba.getUserId());
            }
            return ids;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public boolean isUserAdmin(Long knowledgeBaseId, Long userId) {
        String sql = "SELECT * FROM knowledgebase_admin WHERE knowledge_base_id = ? AND user_id = ?";
        try {
            KnowledgeBaseAdmin result = executeQueryBean(KnowledgeBaseAdmin.class, sql, knowledgeBaseId, userId);
            return result != null;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
